/*
 * Copyright (C) 2012 Caldia Apps.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.caldia.android.frameworkG1.activity;

import java.util.Set;

import org.caldia.android.frameworkG1.application.Application;
import org.caldia.android.frameworkG1.controller.TransitionManager;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;

/**
 * 全画面の親クラス
 * 
 * @author k-matsuda@caldia.org
 * 
 */
public abstract class AbstractActivity extends FragmentActivity {
	/**
	 * 表示させる Fragment クラス名
	 */
	public static final String EXTRA_FRAGMENT_CLASS_NAME = "EXTRA_FRAGMENT_CLASS_NAME";

	/**
	 * デバッグモードフラグ
	 */
	private boolean isDebugMode;

	@Override
	protected void onCreate(final Bundle savedInstanceState) {
		isDebugMode = ((Application) getApplication()).isDebugMode();
		super.onCreate(savedInstanceState);

		TransitionManager.getInstance((Application) getApplication(), -1)
				.sendOnCreateMessage(this);

	}

	@Override
	protected void onResume() {
		super.onResume();
		TransitionManager.getInstance((Application) getApplication(), -1)
				.sendOnResumeMessage(this);

	}

	@Override
	protected void onStop() {
		super.onStop();
		TransitionManager.getInstance((Application) getApplication(), -1)
				.sendOnStopMessage(this);
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		TransitionManager.getInstance((Application) getApplication(), -1)
				.sendOnDestroyMessage(this);
	}

	/**
	 * デバッグモードフラグ取得
	 * 
	 * @return true デバッグモード
	 */
	protected boolean isDebugMode() {
		return isDebugMode;
	}

	/**
	 * Fragment 取得
	 * 
	 * @param id
	 *            ID
	 * @return Fragment
	 */
	protected Fragment findFragmentById(final int id) {
		final FragmentManager manager = getFragmentManager();
		return manager.findFragmentById(id);
	}

	/**
	 * FragmentManager 取得
	 * 
	 * @return FragmentManager
	 */
	protected FragmentManager getFragmentManager() {
		return getSupportFragmentManager();
	}

	/**
	 * Fragment 取得
	 * 
	 * @param tag
	 *            Tag
	 * @return Fragment
	 */
	protected Fragment findFragmentByTag(final String tag) {
		final FragmentManager manager = getFragmentManager();
		return manager.findFragmentByTag(tag);
	}

	/**
	 * IntentからActionを取得する
	 * 
	 * @return アクション文字列
	 */
	protected String getAction() {
		final Intent intent = getIntent();
		if (intent != null) {
			return intent.getAction();
		}
		return null;
	}

	/**
	 * Intentからカテゴリーを取得する
	 * 
	 * @return カテゴリー文字列
	 */
	protected Set<String> getCategory() {
		final Intent intent = getIntent();
		if (intent != null) {
			return intent.getCategories();
		}
		return null;
	}

	/**
	 * 暗黙的Intent作成（自分のパッケージ指定)
	 * 
	 * @param action
	 *            Intent-filter アクション
	 * @param category
	 *            Intent-filter カテゴリ
	 * @return Intent
	 */
	protected Intent createIntent(final String action, final String category) {
		final Intent intent = new Intent(action);
		intent.addCategory(category);
		intent.setPackage(getPackageName());
		return intent;
	}

}
